﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Sandbox.Scene;
using System.Windows;
using GameLib;
using System.Windows.Input;
using GameLib.WorldMap;

namespace Sandbox.Editor.Modes
{
	public class EditorMode
	{
		public EditorMode(EditorWindow window)
		{
			Window = window;
		}

		protected EditorWindow Window
		{
			get;
			private set;
		}

		public virtual void Begin()
		{

		}

		public virtual void End()
		{

		}

		public virtual void MapWindow_MouseLeftButtonDown(MouseButtonEventArgs e)
		{

		}

		public virtual void MapWindow_MouseLeftButtonUp(MouseButtonEventArgs e)
		{
		}

		public virtual void MapWindow_MouseMove(MouseEventArgs e)
		{
			ShowMapCursor(e);
		}

		public virtual void MapWindow_GainFocus(MouseEventArgs e)
		{
			ShowMapCursor(e);
		}

		public virtual void MapWindow_LoseFocus(MouseEventArgs e)
		{
			HideMapCursor();
		}

		public virtual void MapWindow_MouseWheel(MouseWheelEventArgs e)
		{

		}

		private void ShowMapCursor(MouseEventArgs e)
		{
			Point tileCoord = MapScene.GetTileCoordinateAt(e.GetPosition(Window.MapWindow));
			MapScene.ShowCursor((int)tileCoord.X, (int)tileCoord.Y);
			Tile tile = MapScene.Map.GetTile((int)tileCoord.X, (int)tileCoord.Y);

			if (tile != MouseOverTile)
			{
				MouseOverTile = tile;
				if (MouseOverTile != null)
					OnEnterTile(MouseOverTile);
			}
		}

		private void HideMapCursor()
		{
			MapScene.HideCursor();
			MouseOverTile = null;
		}

		protected virtual void OnEnterTile(Tile tile)
		{
		}

		protected Tile MouseOverTile
		{
			get { return _MouseOverTile; }
			private set
			{
				if (_MouseOverTile != value)
				{
					_MouseOverTile = value;
					Window.TextTileCoord.Text = value != null ? string.Format("{0},{1}", value.X, value.Y) : "none";
				}
			}

		}

		private Tile _MouseOverTile = null;

		protected MapScene MapScene
		{
			get { return Window.MapScene; }
		}
	}
}
